﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;

namespace DONGACS.Assembler.Base
{
    public abstract class AbstractGenericAssembler<TDto, TDomain> : IAssembler<TDto, TDomain>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AbstractGenericAssembler&lt;TDto, TDomain&gt;"/> class.
        /// </summary>
        protected AbstractGenericAssembler()
        {
            Mapper.CreateMap<TDomain, TDto>();
            Mapper.CreateMap<TDto, TDomain>();
        }

        /// <summary>
        /// Creates the data transfer object.
        /// </summary>
        /// <param name="domain">The domain to convert.</param>
        /// <returns>The TDto type</returns>
        public virtual TDto ConvertToDataTransferObject(TDomain domain)
        {
            return Mapper.Map<TDomain, TDto>(domain);
        }

        /// <summary>
        /// Creates the domain object.
        /// </summary>
        /// <param name="dto">The dto to convert.</param>
        /// <returns>The TDomain type</returns>
        public virtual TDomain ConvertToBusinessObject(TDto dto)
        {
            return Mapper.Map<TDto, TDomain>(dto);
        }

        /// <summary>
        /// Creates the domain object list.
        /// </summary>
        /// <param name="dtos">The dtos to convert.</param>
        /// <returns>TDomain list</returns>
        /// <exception cref="System.NotSupportedException"></exception>
        public virtual IList<TDomain> ConvertToBusinessObjectList(IList<TDto> dtos)
        {
            IList<TDomain> domains = new List<TDomain>();
            foreach (TDto dto in dtos)
            {
                domains.Add(this.ConvertToBusinessObject(dto));
            }

            return domains;
        }

        /// <summary>
        /// Creates the data transfer object list.
        /// </summary>
        /// <param name="domains">The domains.</param>
        /// <returns>TDto list.</returns>
        /// <exception cref="System.NotSupportedException"></exception>
        public virtual IList<TDto> ConvertToDataTransferObjectList(IList<TDomain> domains)
        {
            IList<TDto> dtos = new List<TDto>();
            foreach (TDomain domain in domains)
            {
                dtos.Add(this.ConvertToDataTransferObject(domain));
            }

            return dtos;
        }
    }
}
